### This demo gives a short overview of the functionality of the stw package,
### without worrying about the sensibility of doing so, the exact results, visualization, etc.


## We need some functions from the ptw package for baseline removal, smoothing, etc.
library(ptw)

## Load GreenTea data
data(GreenTea)

## Remove baseline
GreenTea <- baseline.corr(GreenTea)

## Functionality of stw package is the same as for ptw package
## We can align one sample with one reference:
ref <- GreenTea[1,]
samp <- GreenTea[2,]
one.on.one <- stw(ref,samp)
dim(one.on.one$warp.fun)[1] # 1

## Or we can align several samples with one reference:
samps <- GreenTea[2:9,] # ref stays the same
several.on.one <- stw(ref, samps)
dim(several.on.one$warp.fun)[1] # 8

## Or we can align several samples with an equal number of references:
refs <- GreenTea[1:4,]
samps <- GreenTea[5:8,]
several.on.several <- stw(refs, samps)
dim(several.on.several$warp.fun)[1] # 4

## And, finally, we can do a 'global' alignment, warping multiple traces with ONE (consensus) warping function
ref <- GreenTea[1:4,] # one reference, consisting of multiple traces
samp <- GreenTea[5:8,] # one sample, consisting of an equal number of traces
several.on.several.global <- stw(refs, samps, warp.type="global") # default is "individual"
dim(several.on.several.global$warp.fun)[1] # 1